package models;

import java.util.LinkedList;

import javax.persistence.*;
import play.data.validation.*;
import play.db.jpa.Model;

//Created by C4 , Arwa Ismail
// This model is to create a rating to be added to a plan/request/idea. 
//A rating has a user that did the rating, and the value of the rating itself.
@Entity
public class Rating extends Model {
	@Required
	private User rater; // The user that rated the idea/plan/request. Arwa
						// Ismail
	@Required
	private String rating; // The rating itself that the user chose, whether
							// high,medium or low. Arwa Ismail
	@ManyToOne
	private Idea idea;
	@ManyToOne
	private Request request;
	@ManyToOne
	private Plan plan;
	
	/**
	 * Creates a constructor for Rating that takes the rater User 
	 and the type of rating as inputs and initializes a Rating in the database
	 *
	 * @author      Arwa Ismail
	 * @version     2010.0429    
	 * @category   C4S10
	 * @params     rater:  The user who entered the rating; rating: the rating itelf. 
	*/

	/**
	 * Creates a constructor for Rating that takes the rater User and the type
	 * of rating as inputs and initializes a Rating in the database
	 * 
	 * @author Arwa Ismail
	 * @version 2010.0429
	 * @category C4S10
	 * @params rater: The user who entered the rating; rating: the rating itelf.
	 */


	public Rating(User rater, String rating, String type, long modelID) {
		this.rater = rater;
		this.rating = rating;

		if (type.equalsIgnoreCase("Idea")) {
			Idea idea = Idea.findById(modelID);
			this.idea = idea;
			this.idea.getRatings().add(this);
		} else {
			if (type.equalsIgnoreCase("Plan")) {
				Plan plan = Plan.findById(modelID);
				this.plan = plan;
				this.plan.getRatings().add(this);
			} else {
				if (type.equalsIgnoreCase("Request")) {
					Request request = Request.findById(modelID);
					this.request = request;
					this.request.getRatings().add(this);
				}
			}
		}
		this.save();
	}
	
	public Rating() {}

	public User getRater() {
		return rater;
	}

	public void setRater(User rater) {
		this.rater = rater;
	}

	public String getRating() {
		return rating;
	}

	public void setRating(String rating) {
		this.rating = rating;
	}

	public Idea getIdea() {
		return idea;
	}

	public void setIdea(Idea idea) {
		this.idea = idea;
	}

	public Request getRequest() {
		return request;
	}

	public void setRequest(Request request) {
		this.request = request;
	}

	public Plan getPlan() {
		return plan;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}

}